package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.BusinessCardMapperExt;
import one.stand.mapper.CustomerMapper;
import one.stand.mapper.InputDealMoneyMapper;
import one.stand.mapper.UserTaskMapper;
import one.stand.model.CustomerModel;
import one.stand.model.ResultModel;
import one.stand.model.UserTaskModel;
import one.stand.request.CustomerRequest;
import one.stand.request.UserTaskRequest;
import one.stand.service.UserTaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class UserTaskServiceImpl implements UserTaskService {
    @Autowired
    protected BusinessCardMapperExt businessCardMapperExt;
    /**
     * 企业成员本月目标
     */
    @Autowired
    protected UserTaskMapper userTaskMapper;
    /**
     * 录入成交金额
     */
    @Autowired
    protected InputDealMoneyMapper inputDealMoneyMapper;
    /**
     * 客户关系
     */
    @Autowired
    protected CustomerMapper customerMapper;

    @Override
    public ResultModel addMoneyNum(UserTaskRequest request) {
        log.info("设定员工月目标：{}", JSON.toJSONString(request));
        if (request == null || request.getCardId() == null) {
            return ResultModel.checkParamFail();
        }
        if (request.getRoleId() <= 0 && !request.getMyCardId().equals(request.getCardId())) {
            return ResultModel.fail("你不是老板，无法设定成员目标");
        }
        if (request.getCustomerNum() == null) {
            request.setCustomerNum(0);
        }
        if (request.getTargetAmount() == null) {
            request.setTargetAmount(0);
        }
        UserTaskModel model1 = userTaskMapper.select(new UserTaskModel() {{
            setCardId(request.getCardId());
            setCreateTime(new Date());
        }});
        UserTaskModel model = new UserTaskModel();
        BeanUtils.copyProperties(request, model);
        // 查询本月已录入的成交金额
        Integer completeCustomerNum = inputDealMoneyMapper.selectDelMoney(request.getCardId());
        // 查询本月已录入的客户数
        Integer completeTargetAmount = customerMapper.selectCustomerNum(request.getCardId());
        // 完成状态1未完成  2已完成
        if (completeCustomerNum >= request.getCustomerNum() && completeTargetAmount >= request.getTargetAmount()) {
            model.setCompletionStatus(2);
        } else {
            model.setCompletionStatus(1);
        }
        // 完成金额数
        model.setCompleteCustomerNum(completeCustomerNum);
        // 完成客户数
        model.setCompleteTargetAmount(completeTargetAmount);
        if (model1 != null) {
            model.setTaskId(model1.getTaskId());
            int r = userTaskMapper.updateByPrimaryKeySelective(model);
            if (r > 0) {
                return ResultModel.success();
            }
        } else {
            model.setCompanyId(request.getCId().intValue());
            model.setCreateUserId(request.getId().intValue());
            model.setIsDel(0);
            model.setCreateTime(new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            // 得到当前月最后一天
            model.setEndTime(calendar.getTime());
            model.setCardId(request.getCardId());
            int r = userTaskMapper.insertSelective(model);
            if (r > 0) {
                return ResultModel.success();
            }
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel moveCustomer(CustomerRequest request) {
        log.info("移交客户：{}", JSON.toJSONString(request));
        if (request == null || request.getCustomerIds() == null || request.getCustomerState() == null) {
            return ResultModel.checkParamFail();
        }
        if (request.getRoleId() != null && request.getRoleId() <= 0) {
            List<CustomerModel> list = customerMapper.selectCustomerAllInfo(new CustomerModel() {{
                setIds(request.getCustomerIds().split(","));
            }});
            for (CustomerModel model : list) {
                boolean fal = !request.getMyCardId().equals(model.getAscriptionId().intValue());
                if (fal) {
                    return ResultModel.fail("员工只能移交自己的独占客户~！");
                }
            }
        }
        boolean fal = request.getAscriptionId() != null && request.getMyCardId().equals(request.getAscriptionId())
                && request.getCustomerState() == 1;
        if (fal) {
            return ResultModel.fail("客户不能移交给自己~！");
        }
        CustomerModel model = new CustomerModel();
        model.setIds(request.getCustomerIds().split(","));
        model.setCustomerState(request.getCustomerState());
        if (request.getCustomerState() == 1) {
            model.setAscriptionId(request.getAscriptionId());
        }
        customerMapper.updateMoveCustomer(model);
        return ResultModel.success();
    }
}
